Templates

Resources

1
2
3
4
5
6
Resources:
MyEC2Instance:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-a4c7edb2
InstanceType: t2.micro

Parameters

1
2
3
4
5
6
7
8
9
10
Parameters:
MyInstanceType:
Type: String

Resources:
MyEC2Instance:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-a4c7edb2
InstanceType: !Ref MyInstanceType
  • Pseudo Parameters
    • AWS::AccountId
    • AWS::NotificationARNs
    • AWS::NoValue
    • AWS::Region
    • AWS::StackId
    • AWS::StackName

Mappings

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Mappings:
MyRegionImageId:
us-east-1:
'32': ami-6411e20d
'64': ami-7a11e213
us-west-1:
'32': ami-c9c7978c
'64': ami-cfc7978a

Resources:
MyEC2Instance:
Type: AWS::EC2::Instance
Properties:
ImageId: !FindInMap
- MyRegionImageId
- !Ref AWS::Region
- '32'
InstanceType: t2.micro

Outputs

1
2
3
4
5
6
7
8
9
10
11
12
# Stack 1
Resources:
MyEC2Instance:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-a4c7edb2
InstanceType: t2.micro

Outputs:
Value: !Ref MyEC2Instance
Export:
Name: MyEC2InstanceOutput
1
2
3
4
5
# Stack 2
Resources:
Type: AWS::EC2::EIP
Properties:
InstanceId: !ImportValue MyEC2InstanceOutput

Conditions

1
2
3
4
5
6
7
8
9
10
11
12
Conditions:
CreateEC2Instance: !Equals
- !Ref AWS::Region
- us-east-1

Resources:
MyEC2Instance:
Type: AWS::EC2::Instance
Condition: CreateEC2Instance
Properties:
ImageId: ami-a4c7edb2
InstanceType: t2.micro

Intrinsic Functions

Fn::Ref

1
2
# Ref the resource id
!Ref MyEC2Instance

Fn::GetAtt

1
2
# Get an attribute of a resource
!GetAtt MyEC2Instance.AvailabilityZone

Fn::FindInMap

1
2
3
4
5
# Find a value from a mapping
!FindInMap
- MyRegionImageId
- !Ref AWS::Region
- '32'

Fn::ImportValue

1
2
# Get a exported value from other templates
!ImportValue MyEC2InstanceOutput

Fn::Join

1
2
3
4
5
# Join values into a string
!Join
- ::
- - AWS
- Region

Fn::Sub

1
2
3
4
5
# substitute
!Sub
- ${hostname}.${domain}
- hostname: aws
domain: amazon.com

Condition Functions

  • Fn::And
  • Fn::Equals
  • Fn::If
  • Fn::Not
  • Fn::Or

Others

Rollback

  • If stack creation failed, it will rollback to the previous version automatically
    • Can be disabled
    • If no previous, it will be deleted

Drift

  • Detect if the resources created by CloudFormation is modified (configured different from the CloudFormation template)